package com.snopy.concurrency.interrupt;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname InterruptLockThread
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/22 20:43
 */
@Slf4j
public class InterruptLockThread {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{

            try {
                log.info("thread t1 sleep-----");
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(()->{
            try {
                log.info("thread t2 sleep-----");
                TimeUnit.SECONDS.sleep(1);
                log.info("thread t2 intterupt t1-----");
                t1.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t3 = new Thread(()->{
            while (true){
                log.info("---------------------------------:{}",1);
               boolean flag =  Thread.currentThread().isInterrupted();
               if (flag){
                   //break;
               }
            }

        });

        //t1.start();
        //t2.start();
        t3.start();
        TimeUnit.SECONDS.sleep(1);
        log.info("-----------打断t3");
        t3.interrupt();
        log.info("t3 state:{}",t3.isInterrupted());
        //stop()强制停止一个线程，如果一个资源没有被释放，那就永远不会被释放了
        t3.stop();
        //强制关闭一个进程，当然，也连同进程里的线程
        System.exit(0);
    }
}
